/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpiclient;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
/**
 * @date 12 avr. 2011
 * @time 11:52:53
 * @author Luis.HAFNER
 */
public class VueClient extends JFrame {
    
    private ModeleClient modeleClient;
    private ControleurClient controlleurClient;

    public VueClient() {
        modeleClient = new ModeleClient();
        controlleurClient = new ControleurClient(this,modeleClient);

        this.initFrame();

    }

    private void initFrame() {
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Client");
        // Panneau principal
        JPanel mainPanel = new JPanel();
        // Panneaux interne au principal
        JPanel centerPanel = initCenter();
        JPanel eastPanel = initEast();
        // Gestionnaire de placement principal
        BorderLayout bl = new BorderLayout();
        bl.setHgap(10);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 10, 10));
        mainPanel.setLayout(bl);
        // Ajout des panneaux internes
        mainPanel.add(centerPanel,BorderLayout.CENTER);
        mainPanel.add(eastPanel,BorderLayout.EAST);
        // Ajout du panneau principal
        this.setContentPane(mainPanel);
        this.pack();
    }

    private JPanel initCenter() {
        JPanel centerPanel =new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));

        this.texteEnvoye = new JTextArea(10,5);
        this.texteEnvoye.setLineWrap(true);
        JScrollPane areaScrollPane = new JScrollPane(this.texteEnvoye);
        //areaScrollPane.setVerticalScrollBar(null);

        this.texteEnvoye.setBorder(BorderFactory.createLineBorder(Color.blue));
        areaScrollPane.setPreferredSize(new Dimension(300,200));

        this.champEnvoi = new JTextField();

        this.boutonEnvoyer = new JButton("                                      Envoyer                                     ");
        this.boutonEnvoyer.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.boutonEnvoyer.addActionListener(this.controlleurClient);
        this.boutonEnvoyer.setEnabled(false);
      
        centerPanel.add(areaScrollPane);
        centerPanel.add(this.champEnvoi);
        centerPanel.add(this.boutonEnvoyer);
        return centerPanel;
    }
    private JPanel initEast() {
        JPanel EastPanel =new JPanel();
        BoxLayout boxLayout = new BoxLayout(EastPanel,BoxLayout.Y_AXIS);
        EastPanel.setLayout(boxLayout);

        JLabel labelNom = new JLabel("Login");
        this.champNom = new JTextField();
        this.champNom.setText(System.getProperty("user.name"));

        JLabel labelIP = new JLabel("IP");
        this.champIP = new JTextField();
        this.champIP.setText("127.0.0.1");

        JLabel labelPort = new JLabel("Port");
        this.champPort = new JTextField();
        this.champPort.setText("50000");

        this.boutonConnecter = new JButton("Connecter");
        this.boutonConnecter.addActionListener(this.controlleurClient);

        this.boutonQuitter = new JButton("Quitter");
        this.boutonQuitter.addActionListener(this.controlleurClient);
        
        EastPanel.add(labelNom);
        EastPanel.add(this.champNom);
        EastPanel.add(labelIP);
        EastPanel.add(this.champIP);
        EastPanel.add(labelPort);
        EastPanel.add(this.champPort);

        EastPanel.add(this.boutonConnecter);
        EastPanel.add(Box.createRigidArea(new Dimension(0,50)));
        EastPanel.add(boutonQuitter);
        return EastPanel;      
    }

    public void changerEtatBoutonDemarrer(){
        if(this.boutonConnecter.getText().equals("Connecter"))
            this.boutonConnecter.setText("Déconnecter");
        else
            this.boutonConnecter.setText("Connecter");
    }

    public void messageBox(String texte){
        JOptionPane.showMessageDialog(null, texte);
    }

    public void changerEtatBoutonEnvoyer(){
        this.boutonEnvoyer.setEnabled(!this.boutonEnvoyer.isEnabled());
    }
    
    public JTextField getChampNom() {
        return champNom;
    }
    public JButton getBoutonConnecter() {
        return boutonConnecter;
    }

    public JButton getBoutonEnvoyer() {
        return boutonEnvoyer;
    }

    public JButton getBoutonQuitter() {
        return boutonQuitter;
    }

    public JTextField getChampEnvoi() {
        return champEnvoi;
    }

    public JTextField getChampIP() {
        if(this.champIP.getText().isEmpty())
            this.champIP.setText("127.0.0.1");
       return champIP;
    }
    public void setChampIP(JTextField champIP) {
        this.champIP = champIP;
    }

    public JTextField getChampPort() {
        return champPort;
    }

    public JTextArea getTexteEnvoye() {
        return texteEnvoye;
    }

    // Composants
    private JButton boutonEnvoyer;
    private JTextField champNom;
    private JButton boutonConnecter;
    private JTextArea texteEnvoye;
    private JButton boutonQuitter;
    private JTextField champIP;
    private JTextField champPort;
    private JTextField champEnvoi;
}

